package fishing;
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class FishingServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		/*
		 * Simple greeting: 
		
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello Nir, Oded & the rest of the world"); */
		
		/* Integrating with Google User service */
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
        	// If user is authenticated - show a greeting
            resp.setContentType("text/plain");
            resp.getWriter().println("Hello, " + user.getNickname());
            resp.getWriter().println("");
            resp.getWriter().println("I know your name through the google authentication...");
        } else {
        	// Else - authenticate user and come back again
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }
	}
}
